#ifndef __HAL_TDS_H
#define __HAL_TDS_H

#include "main.h"
//-----------------------------------------------------------
#define TDS_CHECK_PERIOD_CTRL	1	/*采样周期控制*/
#define TDS_VOLTAGE_ZERO		3	/*归零输出*/
#define TDS_VOLTAGE_HOLD		5	/*电压输出保持*/
#define TDS_VOLTAGE_FWD			7	/*正向输出*/
#define TDS_VOLTAGE_REV			9	/*反向输出*/
#define TDS_VOLAD_CHECK			11	/*电压AD采样*/
#define TDS_TEMP_CHECK			13	/*温度采集*/
#define TDS_OTTEMP_CHECK		15	/*其他温度采集*/
#define TDS_ONE_CHECK_PERIOD	400	/*采样周期*/

//-----------------------------------------------------------
#define TDS_ONE_CHECK_PERIOD_SET (20)

#define TDS_ADC_NUM_MAX (22)

#define TDS_MIN_MUL10 (1*10)
#define TDS_MAX_MUL10 (1369*10)
#define TDS_SHORT_ADC (800)//拓邦500
#define TDS_OPEN_ADC  (4090)

//-----------------------------------------------------------
typedef enum
{
	TDSSIGN_ENUM_ST,
	TDSSIGN_ENUM_I,
	TDSSIGN_ENUM_II,
	TDSSIGN_ENUM_III,
	TDSSIGN_ENUM_IV,
	TDSSIGN_ENUM_V,
	TDSSIGN_ENUM_VI,
	TDSSIGN_ENUM_VII = TDS_ONE_CHECK_PERIOD_SET - 1,
	TDSSIGN_ENUM_VIII,
	TDSSIGN_ENUM_IX,
	TDSSIGN_ENUM_X,
	TDSSIGN_ENUM_END,
	
} teTdsSign;
typedef enum
{
	TDS_OVER_WATER= 0, //水箱溢水位
	
	TDS_PURE,              //纯水TDS //tds2
	TDS_SOURCE,			       //源水 //tds1
	TDS_LEAK,              //漏水
	
	TDS_HEAT_L,     			//热罐低
	TDS_HEAT_M,						//热罐中
	TDS_HEAT_H,						//热罐高  
	
	TDS_CHAN_MAX,
}teTdsChan;

typedef enum
{
	WATER_LEAK_NO = 0,
	WATER_LEAK_YES,
	
} tePuerWaterLeakStatus; 


#define TDS_START_13MIN   780//前13分钟只有最后10s有效 
#define TDS_LAST_10S_BUFF_CNT 10//10s求一次平均值 
#define TDS_LAST_2MIN_BUFF_CNT 120//最近2min缓存区 

typedef struct
{
	// uint16_t tds_pulse_cur_cnt;
	// uint16_t tds_pulse_pre_cnt;
	// uint16_t tds_pulse_cycle_sum;

	// uint16_t tds_pulse_sampling_interval_cnt;
	// uint16_t tds_pulse_sampling_times_cnt;
	// uint16_t tds_pulse_sampling_buffer[FIL_TIMES + 1];

	// bool is_enable_tds_fre_get;
	// bool is_tds_val_get_finish;

	// uint16_t tds_water_check_fre;
	const uint16_t (*tds_check_ad2ppm)[2];        //必须放在这里  或者能保证所有的字节能对齐 否则出现 调用是地址变掉
	uint16_t tds_water_value;
	uint8_t  isTDSCheckEn ;          //是否开启tds检测
	
  uint16_t tdsCheckNum ;           //TDS的检测次数
	uint8_t  tdsTableNum;           //控制转换列表的数组
	uint8_t  tdsTableNumBuf;        //缓存控制转换列表的数组

	uint16_t tdsADCValueBuf[2];     //采样2次记录值
	uint8_t  serial;                //序号
	
	uint16_t tdsADCValue[TDS_ADC_NUM_MAX] __attribute__ ((aligned (4))); //采样22次的记录
	uint8_t  tdsADCValueNum;    //采样的序号

	
	uint16_t TdsLast2MinBuff[TDS_LAST_2MIN_BUFF_CNT];//最后2min采集缓存区
	uint16_t TdsLast2MinBuffIndex;//TDS_MAX_TEMPBUFF_CNT s求一次平均值
	uint16_t TdsLast10sBuff[TDS_LAST_10S_BUFF_CNT];//最后10s缓存区
	uint16_t TdsLast10sBuffIndex;//10s缓存区位置标识
	uint32_t TdsTotalSumValue;//总TDS值
	uint32_t TdsTotalSumCnt;//总TDS累计数量
//	uint16_t TdsTempTotalValue;
	uint16_t TdsTimeCnt;//单位s  TDS累计启用时间    
	uint32_t TdsIdleTimeCnt;//水满时间  @7制水间隔小于1小时不频繁上报的时间累计值
//	tsTdsSignStr tds_sign_str;
//	uint8_t checkFlag;//用于波形输出
	
}tsHalTdsCtrl;
//__attribute__ ((aligned (4)))

//extern tePuerWaterLeakStatus hal_tds_check_water_get_leak_status(void);
extern void OnWriteTDSCheckEnable(teTdsChan i);   //使能tds检测
extern void HAL_ATIM_TDSCallback(void);        //250us  定时器6
extern uint16_t OnReadTDSValue(teTdsChan tdsChan); //获取TDS的值供外部调用
extern void OnHalTDSInit(void);
extern uint16_t OnHalTdsADCAverValue(teTdsChan tdsChan);
extern void hal_test(void);
extern uint32_t OnGetTDSError(teTdsChan tds_chan);//获取TDS故障
extern uint16_t OnGetTDSTimeCnt(teTdsChan i);  //获取计时
extern uint16_t OnGetTDSValue(teTdsChan tds_chan); //给纯水TDS和源水TDS调用    
extern void OnHalTDSClear(uint8_t initflag,teTdsChan tdsChan);//参数清除 initflag 1:代表一次制水  先把TdsIdleTimeCnt 默认给拉高到4000   0：代表不是第一次
extern void OnTDSIdelTimeInc(void); 										//满水后空闲的时间累加
extern uint32_t OnTDSGetIdelTime(teTdsChan tds_chan);   //满水后空闲的时间
#endif

